package com.jml.SessionAttribute;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

@Controller
@RequestMapping("/sessionAttribute")
//他将会对名字是"book","description"的参数和类型是Double.class的参数使用
@SessionAttributes(value = {"book","description"},types = {Double.class})
public class SessionAttributeController {

    @RequestMapping("/index")
    public String index(Model model){
        System.out.println("method begin:index");
        model.addAttribute("book","金刚经");
        model.addAttribute("description","佛教金典");
        model.addAttribute("price",new Double(999.99));
        System.out.println("method end:index");
        return "redirect:get";
    }

    /**
     *
     * @param books
     * @param modelMap
     * @param sessionStatus:是来自ModelAndViewContainer中的参数
     * @return
     */
    @RequestMapping("/get")
    public String getBySessionAttributes(@ModelAttribute("book")String books,
                      ModelMap modelMap, SessionStatus sessionStatus){
        System.out.println("method begin:get");
        System.out.println(books);
        System.out.println("----------------------------------------");
        System.out.println(modelMap.get("book"));
        System.out.println(modelMap.get("description"));
        System.out.println(modelMap.get("price"));
        //SessionAttributes已经使用完毕了然后清理缓存表示
        sessionStatus.setComplete();
        System.out.println("method end:get");
        return "redirect:complete";
    }

    @RequestMapping("/complete")
    public String afterComplete(ModelMap modelMap){
        System.out.println("method begin:complete");
        System.out.println(modelMap.get("book"));
        System.out.println(modelMap.get("description"));
        System.out.println(modelMap.get("price"));
        System.out.println("method end:complete");
        return "redirect:index";
    }

}
